package com.zzb.myshowme.view.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Process;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zzb.myshowme.MyApplication;
import com.zzb.myshowme.R;
import com.zzb.myshowme.utils.UserInfoUtils;
import com.zzb.myshowme.utils.DataCleanUtil;
import com.zzb.myshowme.utils.SharedPreferencesUtil;
import com.zzb.myshowme.utils.AccountUtils;

public class SettingActivity extends BaseActivity implements View.OnClickListener {

    ImageView back_imageV;

    RelativeLayout language_layout, reminder_layout, feedback_layout;
    RelativeLayout version_layout, clear_cache_layout, logout_layout;

    TextView language_textV, cache_textV;
    ImageView allow_imageV;

    private int checkedItem = -1;//设置语言

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initView();
    }

    private void initView() {
        back_imageV = (ImageView) findViewById(R.id.back_imageV);

        language_layout = (RelativeLayout) findViewById(R.id.language_layout);
        reminder_layout = (RelativeLayout) findViewById(R.id.reminder_layout);
        feedback_layout = (RelativeLayout) findViewById(R.id.feedback_layout);
        version_layout = (RelativeLayout) findViewById(R.id.version_layout);
        clear_cache_layout = (RelativeLayout) findViewById(R.id.clear_cache_layout);
        logout_layout = (RelativeLayout) findViewById(R.id.logout_layout);

        language_textV = (TextView) findViewById(R.id.language_textV);
        allow_imageV = (ImageView) findViewById(R.id.allow_imageV);
        cache_textV = (TextView) findViewById(R.id.cache_textV);
        try{
            cache_textV.setText(DataCleanUtil.getTotalCacheSize(this));
        }catch (Exception e) {
            e.printStackTrace();
        }

        back_imageV.setOnClickListener(this);
        language_layout.setOnClickListener(this);
        reminder_layout.setOnClickListener(this);
        feedback_layout.setOnClickListener(this);
        version_layout.setOnClickListener(this);
        clear_cache_layout.setOnClickListener(this);
        logout_layout.setOnClickListener(this);

    }

    private void showLanguageDialog() {
        final String[] items = new String[2];
        items[0] = "中文";
        items[1] = "English";
        final AlertDialog.Builder alert = new AlertDialog.Builder(this);
//        alert.setTitle(getString(R.string.profile_select_sex));
        alert.setTitle("设置语言");
        String sexStr = language_textV.getText().toString().trim();
//        if (getString(R.string.profile_female).equals(sexStr)) {
        if ("中文".equals(sexStr)) {
            checkedItem = 0;
        } else {
            checkedItem = 1;
        }
        alert.setSingleChoiceItems(items, checkedItem, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which != -1) {
                    language_textV.setText(items[which]);
                    checkedItem = which;
                }
                dialog.dismiss();
            }
        });
        alert.show();
    }

    /**
     * 清理缓存
     */
    private void clear() {
        DataCleanUtil.clearAllCache(SettingActivity.this);
        try {
            cache_textV.setText(DataCleanUtil.getTotalCacheSize(this));
            Toast.makeText(SettingActivity.this, "清除缓存成功...", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_imageV:
                finish();
                break;
            case R.id.language_layout:
                showLanguageDialog();
                break;
            case R.id.reminder_layout:
                if (allow_imageV.isSelected()) {
                    allow_imageV.setSelected(false);
                } else {
                    allow_imageV.setSelected(true);
                }
                break;
            case R.id.feedback_layout:
                break;
            case R.id.version_layout:
                break;
            case R.id.clear_cache_layout:
                new AlertDialog.Builder(SettingActivity.this).setTitle("清除缓存")
                        .setMessage("清除缓存后使用的流量可能会额外增加，确定清除？")
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                clear();
                            }
                        }).setNegativeButton("取消", null).show();
                break;
            case R.id.logout_layout:
                new AlertDialog.Builder(SettingActivity.this).setTitle("退出登录")
                        .setMessage("退出登录会清空用户信息，确定退出？")
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                SharedPreferencesUtil.setBoolean(MyApplication.getInstance(), SharedPreferencesUtil.LOGIN, false);//设置登录状态
                                UserInfoUtils.removeLoginMember(MyApplication.getInstance());//删除文件
                                AccountUtils.userCheckOut(MyApplication.getInstance());
                                Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);//开新堆栈，并清空之前的堆栈
                                startActivity(intent);
//                                android.os.Process.killProcess(Process.myPid());
                            }
                        }).setNegativeButton("取消", null).show();
                break;
        }
    }
}
